package com.frank.redis.shard;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.frank.redis.JedisPoolFactory;
import com.frank.redis.spring.JedisPoolBean;
import com.frank.zk.ZookeeperGroup;

/**
 * 
 * @author liushaomingdev@163.com
 *
 */
public class JedisCluster extends ZookeeperGroup<ShardInfo> {

	private String name, password;

	public JedisCluster(String root, String name, String password) {
		super(root, new ShardInfo.ShardInfoFactory());
		this.name = name == null ? JedisPoolBean.DEFAULT_ZKNAME : name;
		this.password = password;
		afterPropertiesSet();
	}

	@Override
	protected void nodeChnage(String node) {
		for (Map.Entry<String, ShardInfo> entry : children.entrySet()) {
			ShardInfo info = entry.getValue();
			info.setName(entry.getKey());

			if (StringUtils.isNotBlank(password))
				info.setPassword(password);
		}
		JedisPoolFactory.loadServers(children.values(), name);
	}
}
